/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-9-23 下午2:41 Created by piaohh
 */
package com.jade.bss.web.interceptor;

import com.jade.bss.admin.account.AdminAccount;
import com.jade.bss.admin.journal.AdminJournal;
import com.jade.bss.base.journal.Journal;
import com.jade.framework.base.util.JsonUtils;
import com.jade.journal.JournalUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 管理员操作日志拦截器
 * @author <a href="mailto:Administrator@ceno.cn">piaohh</a>
 * @version 1.0 15-9-23 下午2:41
 */
public class AdminJournalInterceptor
{
    protected com.jade.journal.Journal journalObj = JournalUtils.getJournal(AdminJournal.NAME);

//    private AdminJournalManager adminJournalManager;
//
//    @Inject
//    public void setAdminJournalManager(@Named ("bss_adminJournalManager") AdminJournalManager adminJournalManager)
//    {
//        this.adminJournalManager = adminJournalManager;
//    }

    public void doReturn(JoinPoint jp)
    {
        AdminJournal adminJournal = new AdminJournal();
        Subject subject = SecurityUtils.getSubject();
        AdminAccount adminAccount = (AdminAccount) subject.getPrincipal();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Method method = ((MethodSignature) jp.getSignature()).getMethod();
        Journal journal = AnnotationUtils.getAnnotation(method, Journal.class);
        adminJournal.setModule(journal.module());
        String methodName = jp.getSignature().getName();
        adminJournal.setAction(methodName);
        adminJournal.setOperator(String.valueOf(adminAccount.getId()));
        adminJournal.setOperatorType(adminAccount.getType());
        adminJournal.setOwnerType(adminAccount.getOwnerType());
        adminJournal.setOwnerId(adminAccount.getOwnerId());
        adminJournal.setAdminName(adminAccount.getName());
        adminJournal.setUserAgent(request.getHeader("User-Agent"));
        adminJournal.setIp(request.getRemoteAddr());
        adminJournal.setUrl(request.getRequestURI());
        adminJournal.setOpDate(new Date());
        adminJournal.setResult(true);
        List<Object> list = new ArrayList<Object>();
        Object[] args = jp.getArgs();
        for (Object obj : args) {
            if (!(obj instanceof HttpServletResponse) && !(obj instanceof HttpServletRequest)) {
                list.add(obj);
            }
        }
        adminJournal.setParams(JsonUtils.writeToString(list));
        //adminJournalManager.add(adminJournal);
        journalObj.infoEntity(adminJournal);
    }

    public void doThrowing(JoinPoint jp, Throwable ex)
    {
        AdminJournal adminJournal = new AdminJournal();
        Subject subject = SecurityUtils.getSubject();
        AdminAccount adminAccount = (AdminAccount) subject.getPrincipal();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Method method = ((MethodSignature) jp.getSignature()).getMethod();
        Journal journal = AnnotationUtils.getAnnotation(method, Journal.class);
        adminJournal.setModule(journal.module());
        String methodName = jp.getSignature().getName();
        adminJournal.setAction(methodName);
        adminJournal.setOperator(String.valueOf(adminAccount.getId()));
        adminJournal.setOperatorType(adminAccount.getType());
        adminJournal.setOwnerType(adminAccount.getOwnerType());
        adminJournal.setOwnerId(adminAccount.getOwnerId());
        adminJournal.setAdminName(adminAccount.getName());
        adminJournal.setUserAgent(request.getRemoteUser());
        adminJournal.setIp(request.getRemoteAddr());
        adminJournal.setUrl(request.getRequestURI());
        adminJournal.setOpDate(new Date());
        adminJournal.setResult(false);
        adminJournal.setErrorCode(ex.getClass().getName());
        adminJournal.setErrorMessage(ex.getMessage());
        adminJournal.setResult(false);
        List<Object> list = new ArrayList<Object>();
        Object[] args = jp.getArgs();
        for (Object obj : args) {
            if (!(obj instanceof HttpServletResponse) && !(obj instanceof HttpServletRequest)) {
                list.add(obj);
            }
        }
        adminJournal.setParams(JsonUtils.writeToString(list));
        journalObj.infoEntity(adminJournal);
        //adminJournalManager.add(adminJournal);
    }
}
